<?php


namespace app\zhengmaotongadmin\controller;
use app\model\BannerModel;
use app\validate\BannerValidate;
use cmf\controller\AdminBaseController;
use think\facade\Db;

class BannerController extends AdminBaseController
{
    public function index(){
        $banner=input('banner','');
        $where=[];
        if($banner){
            $where[]=['url','like',"%$banner%"];
        }
        $admin=Db::name('banner')
            ->order('list_order','desc')
            ->where($where)
            ->paginate(10);
        $this->assign('admin',$admin);
        return $this->fetch();
    }
    public function add(){
        return $this->fetch();
    }
    public function addPost(){
        //接收
        $data=input('post.');
        $validate=new \app\zhengmaotongadmin\validate\BannerValidate();
        $res=$validate->scene('add')->check($data);
        if(!$res){
            $this->error($validate->getError());
        }

        $model=\app\zhengmaotongadmin\model\BannerModel::create($data);
        if($model){
            $this->success('添加成功','Banner/index');
        }else{
            $this->error('添加失败');
        }
    }
    public function edit(){
        $id = input('id');
        $admin = Db::name('banner')
            ->where('id', '=', $id)
            ->find();
        $this->assign('admin', $admin);
        return $this->fetch();
    }
    public function editPost(){
        //接收
        $data=input('post.');
        $validate=new \app\zhengmaotongadmin\validate\BannerValidate();
        $res=$validate->scene('edit')->check($data);
        if(!$res){
            $this->error($validate->getError());
        }
        $model=\app\zhengmaotongadmin\model\BannerModel::find($data['id']);

        $model->url=$data['url'];
        $model->image=$data['image'];
        if($model->save()){
            $this->success('保存成功','Banner/index');
        }else{
            $this->error('保存失败');
        }
    }
    public function del(){
        $id=input('id/d');
        $model=\app\zhengmaotongadmin\model\BannerModel::destroy($id);
        if($model){
            $this->success('删除成功','Banner/index');
        }else{
            $this->error('删除失败');
        }
    }
    //排序
    public function sx(){
        $model=new \app\zhengmaotongadmin\model\BannerModel();
        parent::listOrders($model);
        $this->success('排序成功');
    }
}